import axios from 'axios'
let baseUrl = "http://118.25.181.144:90"
import utils from "@/utils/utils.js";

const request = (options = {}) => {
  return new Promise((resolve, reject) => {
    let obj = {
      url: baseUrl + options.url || '',
      method: options.method.toLocaleUpperCase() || 'GET',
      header: options.header || {}
    }
    switch (options.method) {
      case "get":
        obj.params = options.data || {}
        break;
      case "post":
        obj.data = options.data || {}
        break;
      default:
        break;
    }
    axios(obj).then(data => {
      if (data.data.code == 200) {
        let result = null
        if (data.data.rows) {
          result = {
            rows: data.data.rows,
            total: data.data.total
          }
        } else {
          result = data.data.data
        }
        resolve(result);
      } else {
        utils.showToast(data.data.msg, 0)
        reject(data.data.msg)
      }
    }).catch(error => {
      utils.showToast(error, 0)
      reject(error)
    })
  });
}

const get = (url, data, options = {}) => {
  options.method = 'GET';
  options.data = data;
  options.url = url;
  return request(options)
}

const post = (url, data, options = {}) => {
  options.method = 'POST';
  options.data = data;
  options.url = url;
  return request(options)
}


export default {
  request,
  get,
  post
}
